#!/usr/bin/env node

// See: https://cirosantilli.com/cirodown#build-sass

const fs = require('fs');
const path = require('path');

const katex = require('katex');
// Can't easily require this, its main is the css...
//const normalize_css = require('normalize.css');
const sass = require('sass');

const cirodown_nodejs = require('cirodown/nodejs');
const package_json = fs.readFileSync(cirodown_nodejs.PACKAGE_PACKAGE_JSON_PATH).toString();
const package_json_json = JSON.parse(package_json);

let embed_includes = ``;
for (const local_include of cirodown_nodejs.LOCAL_INCLUDES) {
  embed_includes += `@use "${local_include}";`
}

const external_includes = `
@import "https://cdn.jsdelivr.net/npm/katex@${katex.version}/dist/katex.min.css";
@import "https://cdnjs.cloudflare.com/ajax/libs/normalize/${package_json_json['dependencies']['normalize.css']}/normalize.min.css";
`;

fs.mkdirSync(cirodown_nodejs.PACKAGE_OUT_PATH, {recursive: true})

const scss_input = fs.readFileSync(path.join(__dirname, cirodown_nodejs.PACKAGE_SASS_BASENAME)).toString();
let result;

result = sass.renderSync({
  data: embed_includes + scss_input,
  outputStyle: 'compressed',
});
fs.writeFileSync(cirodown_nodejs.PACKAGE_OUT_CSS_EMBED_PATH, result.css);

result = sass.renderSync({
  data: external_includes + scss_input,
  outputStyle: 'compressed',
});
fs.writeFileSync(cirodown_nodejs.PACKAGE_OUT_CSS_PATH, result.css);

result = sass.renderSync({
  data: scss_input,
  outputStyle: 'compressed',
});
fs.writeFileSync(cirodown_nodejs.PACKAGE_OUT_CSS_LOCAL_PATH, result.css);
